
from multiprocessing import Process, Pipe
from visualisewidget3d import VisualiseWidget3D, SceneNode, ScenePart


class VisualPart:
	def __init__(self, filename, color, position, rotation):
		# Put the part in a SceneNode to preserve the initial pos/rot !
		self.part = SceneNode()
		self.part.add(0, ScenePart(filename = filename,
							  color = color,
							  position = position,
							  rotation = rotation))
	
	def scene_part(self):
		return self.part


class Visualiser:
	def __init__(self, visual_parts):
		self._visual_parts = visual_parts
	
	def start(self):
		# Start visualiser process
		self.parent_conn, child_conn = Pipe()
		p = Process(target=self._runner, args=(child_conn,))
		p.start()
	
	def move(self, name, position):
		self.parent_conn.send(("position", name, position))
	
	def _runner(self, conn):
		vw = VisualiseWidget3D(conn)

		origo = SceneNode()
		origo.make_origo()
		vw.scene.add("origo", origo)

		for part_name in self._visual_parts:
			vw.scene.add(part_name, self._visual_parts[part_name].scene_part())
		
		vw.main()

